﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
namespace webnew.Systems
{
    public partial class Dialog_SelectStaff : WebPageBase.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            UcCompany1.AutoPostBack = true;
            UcCompany1.SelectedIndexChanged += ddlCompanySelectIndexChanged;
            if (!IsPostBack)
            {
               // txtOADept.Style.Add("visibility", "hidden");  //隐藏选中的OA部门ID
               // txtHRDept.Style.Add("visibility", "hidden");  //隐藏选中的HR部门ID           
                //绑定OA单位列表,初始化部门列表
                UcCompany1.show(true, "ALL");
                //绑定HR单位列表,初始化部门列表
                //UcCompany2.show(true, "HR");
                //初始化OA部门和HR部门
                ddlOADept.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));
                if (Request.QueryString["CompId"] != null)
                {
                    UcCompany1.SelectedValue = Request.QueryString["CompId"].ToString();
                    ddlCompanySelectIndexChanged(null, null);
                }
                if (Request.QueryString["DeptId"] != null) ddlOADept.SelectedValue = Request.QueryString["DeptId"].ToString();
                AspNetPager1_PageChanged(null, null);
            }
        }

        protected void ddlCompanySelectIndexChanged(object sender, EventArgs e)
        {
            Common.CommonHelper.DataDdlBind(ddlOADept, GetDeptList(UcCompany1.SelectedValue), "deptname", "id");
            ddlOADept.Items.Insert(0, new ListItem("--请选择--", Guid.Empty.ToString()));

        }

        /// <summary>
        /// 通过单位ID获取部门列表
        /// </summary>       
        public List<Model.Sys_DeptInfo> GetDeptList(string CompID)
        {
            return new BLL.Sys_Dept().GetDeptListByComp(CompID);
        }

        /// <summary>
        /// 检索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            AspNetPager1_PageChanged(null, null);             //刷新页面
        }




        /// <summary>
        /// 分页控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>    
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //分页的查询表名
            System.Text.StringBuilder strtable = new System.Text.StringBuilder();
            //要查询的字段
            System.Text.StringBuilder strFeild = new System.Text.StringBuilder();
            strFeild.Append(" b.compname as OACompName,(select deptname from Sys_dept where id=a.OADeptId) as OADeptName ,a.* ");
            
            strtable.Append(" Sys_Staff a join Sys_Company b on a.OACompId=b.Id   ");

            //strtable.Append(" join Sys_Company d on a.HRCompId=d.Id join Sys_Dept e on a.HRDeptId=e.Id  ");
            //分页的查询的条件  
            System.Text.StringBuilder _conditon = new System.Text.StringBuilder();
            //用户姓名,职称检索条件
            _conditon.Append(" a.truename like '%" + txtTrueName.Text.Trim().Replace("'", "") + "%'");
            _conditon.Append(" and a.titleName like '%" + txtTitleName.Text.Trim().Replace("'", "") + "%'");
            //OA公司,部门检索条件
            if (!(UcCompany1.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OACompId ='" + UcCompany1.SelectedValue + "'");
            if (!(ddlOADept.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.OADeptId ='" + ddlOADept.SelectedValue + "'");
            ////HR公司,部门检索条件
            //if (!(UcCompany2.SelectedValue == Guid.Empty.ToString())) _conditon.Append(" and a.HRCompId ='" + UcCompany2.SelectedValue + "'");
            //if (!(txtHRDept.Text == "0")) _conditon.Append(" and a.HRDeptId ='" + txtHRDept.Text + "'");
            //状态检索条件
            if (!(dllStatus.SelectedValue == "全部"))
            {
                _conditon.Append(" and a.isdel ='" + dllStatus.SelectedValue + "'");
            }
            else _conditon.Append(" and a.isdel <> 1");
            AspNetPager1.RecordCount = new BLL.Sys_Staff().GetCount(strtable.ToString(), _conditon.ToString());             //根据条件获取相关的行数
            webnew.Common.CommonHelper.DataGrdBind(grdView, new BLL.Sys_Staff().GetPageList(strFeild.ToString(), _conditon.ToString(), grdView.PageSize, AspNetPager1.CurrentPageIndex, strtable.ToString(), "b.compname,(select deptname from Sys_dept where id=a.OADeptId),a.TrueName,a.Id", true));
        }


        protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标经过变色
                e.Row.Attributes.Add("onmouseover", "GridViewMouseover(this)");
                e.Row.Attributes.Add("onmouseout", "GridViewMouseout(this)");
                e.Row.Attributes.Add("onclick", "GridViewonclick(this)");
                CheckBox chbox;
                chbox = (CheckBox)e.Row.Cells[0].FindControl("chkitem");
                string staffId = grdView.DataKeys[e.Row.RowIndex].Value.ToString();
                string staffName = e.Row.Cells[1].Text;
                //string strAttr = "self.parent.tb_remove();self.parent.getSelStaffInfo('" + staffId + "','" + staffName + "');";
                string strAttr = "getStaffInfo('" + staffId + "','" + staffName + "');";
                chbox.Attributes.Add("onclick", strAttr);
                
            }
        }
    }
}
